package de.arndt_plc.wakeonmagic;

import android.app.Activity;
import android.app.ActionBar;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;

import java.util.ArrayList;
import java.util.List;


public class WakeOnMagicAppWidgetActivity extends Activity implements ActionBar.OnNavigationListener {

    /**
     * The serialization (saved instance state) Bundle key representing the
     * current dropdown position.
     */
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

    ActionBar mActionBar;
    final List<NavigationListHolder> mWidgets = new ArrayList<NavigationListHolder>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // get all available mWidgets
        List<Integer> widgetIds = WakeOnMagicAppWidgetProvider.getWidgetIds(this);
        if (widgetIds.size() == 0) {
            // no mWidgets found
            setContentView(R.layout.wake_on_magic_app_widget_activity_error);
            return;
        }
        setContentView(R.layout.wake_on_magic_app_widget_configure_activity);

        // Set up the action bar to show a dropdown list.
        mActionBar = getActionBar();
        mActionBar.setDisplayShowTitleEnabled(false);
        mActionBar.setDisplayUseLogoEnabled(false);
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        // get widget names
        mWidgets.clear();
        for (Integer id : widgetIds) {
            WakeOnMagicAppWidgetConfigureFragment.Prefs prefs = WakeOnMagicAppWidgetConfigureFragment.load(this, id);
            if (prefs != null) {
                mWidgets.add(new NavigationListHolder(prefs.Pc, id));
            }
        }

        // Set up the dropdown list navigation in the action bar.
        mActionBar.setListNavigationCallbacks(
                // Specify a SpinnerAdapter to populate the dropdown list.
                new ArrayAdapter<NavigationListHolder>(
                        mActionBar.getThemedContext(),
                        android.R.layout.simple_list_item_1,
                        android.R.id.text1,
                        mWidgets),
                this);
    }

    /*
     define the list holder
     */
    private static class NavigationListHolder {
        final String mName;
        final Integer mWidgetId;

        public NavigationListHolder(String name, Integer widgetId) {
            mName = name;
            mWidgetId = widgetId;
        }

        public Integer getWidgetId() {
            return mWidgetId;
        }

        @Override
        public String toString() {
            return mName;
        }
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Restore the previously serialized current dropdown position.
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            Integer lastPos = savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM);
            if (lastPos < getActionBar().getNavigationItemCount() && lastPos >= 0)
                getActionBar().setSelectedNavigationItem(lastPos);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // Serialize the current dropdown position.
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM,
                getActionBar().getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.wake_on_magic_app_widget_configure_activity, menu);
        return true;
    }

    @Override
    public boolean onNavigationItemSelected(int position, long id) {
        // When the given dropdown item is selected, show its contents in the
        // container view.
        Integer widgetId = mWidgets.get(position).getWidgetId();
        WakeOnMagicAppWidgetConfigureFragment configFragment = WakeOnMagicAppWidgetConfigureFragment.newInstance(widgetId, false);
        getFragmentManager().beginTransaction()
                .replace(R.id.container, configFragment)
                .commit();
        return true;
    }
}
